﻿using System;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;

using MySql.Data.MySqlClient;
using umbraco.blog4.POCO;
using BL=umbraco.BusinessLogic;
using CMSBL=umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.datatype.controls;
using umbraco.cms.businesslogic.packager;
using umbraco.cms.businesslogic.propertytype;
using umbraco.cms.businesslogic.web;
using TheOutfield.UmbExt.UniversalMediaPicker;
using System.Data.EntityClient;
using System.Collections.Generic;
using System.Reflection;

namespace umbraco.blog4.plugins {
    public partial class PostInstall : System.Web.UI.UserControl {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                
            }
        }

        protected void mvwPostInstallSteps_ActiveViewChanged(object sender, EventArgs e) {
            switch (mvwPostInstallSteps.ActiveViewIndex) {
                // Detect Universal Media Picker, add doctype provider, and properties
                case 1:
                    InstalledPackage ump = null;
                    foreach (InstalledPackage ip in InstalledPackage.GetAllInstalledPackages()) {
                        if (ip.Data.Name == "Universal Media Picker") { ump = ip; break; }
                    }
                    if (ump != null) {
                        /* We found it
                         * 1) Create DocumentType provider datatype
                         * 2) Create properties on doctype with datatype
                         * 3) Create blog!
                         */
                        
                        DocumentType blog = DocumentType.GetByAlias("Blog");
                        int dtTab = blog.AddVirtualTab("Document types");

                        // Create document type picker datatype
                        Factory f = new Factory();
                        DataTypeDefinition blogDt = DataTypeDefinition.MakeNew(BL.User.GetCurrent(), "Document type Picker");
                        blogDt.DataType = (from m in f.GetAll() where m.DataTypeName == "Universal Media Picker" select m).First();
                        ((UniversalMediaPickerDataType)blogDt.DataType).Config.ProviderAlias = "Document Type Provider";
                        blogDt.Save();

                        // blog.AddPropertyType(blogDt, "dtBlog", "Blog document type");
                        blog.AddPropertyType(blogDt, "dtBlogPost", "Blog post document type");
                        blog.AddPropertyType(blogDt, "dtBlogTextPage", "Blog text page document type");
                        blog.AddPropertyType(blogDt, "dtComment", "Comment document type");
                        blog.AddPropertyType(blogDt, "dtTrackBack", "TrackBack document type");
                        foreach (PropertyType pt in blog.PropertyTypes) {
                            if (pt.TabId < 0) blog.SetTabOnPropertyType(pt, dtTab);
                        }
                    } else {
                        // Not found - throw to installer.
                    }
                    break;
                // Detect blog, detect comments, port to new system or Disqus
                case 2:
                    CMSBL.Content[] findBlog = CMSBL.Content.getContentOfContentType(CMSBL.ContentType.GetByAlias("Blog"));
                    if (findBlog.Length > 0) {
                        // we have at least one blog
                        string typeName = umbraco.BusinessLogic.Application.SqlHelper.GetType().BaseType.ToString();
                        DbConnection conn;
                        switch (typeName) {
                            case "MySqlHelper":
                                conn = new MySqlConnection(BL.Application.SqlHelper.ConnectionString);
                                break;
                            default:
                                // SqlServerHelper
                                conn = new SqlConnection(BL.Application.SqlHelper.ConnectionString);
                                break;
                        }
                        using (POCO.Entities existingComments = new Entities(
                            new EntityConnection(
                                new System.Data.Metadata.Edm.MetadataWorkspace(
                                    new List<string>() { "res://*/" },
                                    new List<Assembly>() { this.GetType().Assembly }
                                ), conn))) {
                            foreach(POCO.Comment c in existingComments.Comments) {
                                Comment newComment = new Comment((int)c.PostID, c.Name, c.Email, c.Website, c.BodyText) { IsSpam = c.IsSpam.GetValueOrDefault() };
                                if (!newComment.IsSpam) newComment.Publish();
                            }
                        }
                    }
                    break;
                // If blog exists, don't bother, or create new blog.
                case 3:
                    break;
                // First view
                default:
                    break;
            }
        }
    }
}